<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title></title>
    <!-- 这里可以添加CSS样式文件 -->
</head>

<body>
    <script type="importmap">
      {
        "imports": {
          "three": "./threejs/build/three.module.js",
          "three/addons/": "./threejs/examples/jsm/"
        }
      }
    </script>
    <script type="module">
        import * as THREE from "three";
        import { OrbitControls } from "three/addons/controls/OrbitControls.js";
        import Stats from 'three/addons/libs/stats.module.js';

        class ThreeCore {
            dom;
            scene;
            camera;
            defaultCamera;
            renderer;
            clock;
            options;
            stats;
            // 要执行动画的对象集合, 子类可以把自己的动画写进 onRender 也可以 this.addAnimate() 添加到父类动画集合里
            animates;
            constructor(dom, options) {
                this.dom = dom;
                this.options = options;
                this.scene = new THREE.Scene();
                this.clock = new THREE.Clock();
                this.animates = {};
                const k = dom.clientWidth / dom.clientHeight;
                if ("fov" in options.cameraOptions) {
                    this.defaultCamera = new THREE.PerspectiveCamera(options.cameraOptions.fov, k, options.cameraOptions.near, options.cameraOptions.far);
                }
                else {
                    const s = options.cameraOptions.s;
                    this.defaultCamera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, options.cameraOptions.near, options.cameraOptions.far);
                }
                this.camera = this.defaultCamera;
                this.scene.add(this.camera);
                const rendererOptions = {
                    // 抗锯齿
                    antialias: true,
                    alpha: true,
                    // 深度缓冲, 解决模型重叠部分不停闪烁问题
                    // 这个属性会导致精灵材质会被后面的物体遮挡
                    // 只能出现问题的时候, 在那个场景 new ThreeCore继承类的时候, 传入rendererOptions参数, 将此参数改为 false
                    logarithmicDepthBuffer: true
                };
                const renderer = new THREE.WebGLRenderer(Object.assign({}, rendererOptions, options.rendererOptions));
                renderer.setSize(dom.clientWidth, dom.clientHeight);
                renderer.setPixelRatio(window.devicePixelRatio);
                dom.appendChild(renderer.domElement);
                this.renderer = renderer;
                this.stats = new Stats();
                dom.appendChild(this.stats.dom);
                window.addEventListener("resize", this.onResize, false);
                //利用 setTimeout 宏任务最后执行特性, 使js执行过程要等所有微任务和同步代码执行完再执行, 否则 this.init() 可能会在场景未搭建完毕就执行报错或没有生产对象

                this.init();
                this.animate();

            }
            // 提供给子类覆写
            onRenderer() { }
            // 提供给子类覆写
            onDestroy() { }
            animate() {
                this.renderer.setAnimationLoop(() => {
                    this.onRenderer();
                    this.stats.update();
                    // 执行动画
                    for (const key in this.animates) {
                        this.animates[key](this.clock.getDelta());
                    }
                    this.renderer.render(this.scene, this.camera);
                });
            }
            addAnimate(name, func) {
                this.animates[name] = func;
            }
            removeAnimate(name) {
                delete this.animates[name];
            }
            onResize = () => {
                const width = this.dom.clientWidth;
                const height = this.dom.clientHeight;
                const k = width / height;
                // 更新相机
                if (this.camera instanceof THREE.PerspectiveCamera) {
                    this.camera.aspect = k;
                }
                else {
                    const s = this.options.cameraOptions.s;
                    this.camera.left = -s * k;
                    this.camera.right = s * k;
                    this.camera.top = s;
                    this.camera.bottom = -s;
                }
                this.camera.updateProjectionMatrix();
                // 更新renderer
                this.renderer.setSize(width, height);
                this.renderer.setPixelRatio(window.devicePixelRatio);
            };
            destroyed() {
                // 需要手动移除掉 gui, 否则刷新页面时会出现多个gui
                document.querySelector(".dg.main.a")?.remove();
                window.removeEventListener("resize", this.onResize.bind(this));
                this.onDestroy();
                this.renderer.setAnimationLoop(null);
                this.renderer.renderLists.dispose();
                this.renderer.dispose();
                this.renderer.forceContextLoss();
                this.renderer.domElement.innerHTML = "";
                this.scene.clear();
                THREE.Cache.clear();
            }
        }

        class ThreeProject extends ThreeCore {
            orbit;
            shadeMaterial;
            params = {
                uWaresFrequency: 20,
                uScale: 0.1,
                uNoiseFrequency: 40,
                uNoiseScale: 2,
                uXzScale: 2,
                uLowColor: 0x000000,
                uHighColor: 0xffffff,
                uOpacity: 0.5,
            };
            constructor(dom) {
                super(dom, {
                    cameraOptions: {
                        fov: 45,
                        near: 0.1,
                        far: 100
                    }
                });
                this.scene.background = new THREE.Color(0x000000);
                this.camera.position.set(0, 0, 3);
                this.orbit = new OrbitControls(this.camera, this.renderer.domElement);
                const texture = new THREE.TextureLoader().load(FILE_HOST + 'threeExamples/shader/flag.webp');
                const shadeMaterial = new THREE.ShaderMaterial({
                    vertexShader: `precision mediump float;
                    uniform float uTime;
                    varying vec2 vUv;
                    
                    void main() {
                        vUv = uv;
                        vec4 modelPosition = modelMatrix * vec4(position, 1.0);
                        modelPosition.z = sin((modelPosition.x + uTime) * 10.0) * 0.05;
                        modelPosition.z += sin((modelPosition.y + uTime) * 10.0) * 0.05;
                        gl_Position = projectionMatrix * viewMatrix * modelPosition;
                    }`,
                    fragmentShader: `precision mediump float;
                    uniform sampler2D uTexture;
                    varying vec2 vUv;
                    
                    void main() {
                        vec4 textureColor = texture2D(uTexture, vUv);
                        gl_FragColor = textureColor;
                    }`,
                    side: THREE.DoubleSide,
                    uniforms: {
                        uTime: {
                            value: 0
                        },
                        uTexture: {
                            value: texture
                        }
                    }
                });
                this.shadeMaterial = shadeMaterial;
                const plane = new THREE.Mesh(new THREE.PlaneGeometry(2, 1, 128, 128), shadeMaterial);
                this.scene.add(plane);
            }
            init() {
            }
            onRenderer() {
                const elapsed = this.clock.getElapsedTime();
                this.orbit.update();
                this.shadeMaterial.uniforms.uTime.value = elapsed;
            }
        }

        new ThreeProject(document.body);
    </script>
</body>

</html>